package bio.file.charsteam;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * Instruction：
 * Author：@author MaLi
 */
public class FileCopy {
    public static void transferFile(String src, String dest) {
        try (FileReader reader = new FileReader(src);
        FileWriter writer = new FileWriter(dest, StandardCharsets.UTF_8)) {
            int length;
            char[] buffer = new char[8192];
            while ((length = reader.read(buffer))!=-1) {
                writer.write(buffer,0,length);
            }
            writer.flush();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void transferFileBuffered(String src, String dest) {
        try (BufferedReader reader = new BufferedReader(new FileReader(src));
             BufferedWriter writer = new BufferedWriter(new FileWriter(dest, StandardCharsets.UTF_8))) {
            int length;
            char[] buffer = new char[8192];
            while ((length = reader.read(buffer))!=-1) {
                writer.write(buffer,0,length);
            }
            writer.flush();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
